package io.renren.modules.sys.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.R;
import io.renren.common.utils.Tools;
import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.sys.entity.UnitEntity;
import io.renren.modules.sys.service.UnitService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;



/**
 * 多单位表
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2018-11-22 14:29:36
 */
@RestController
@RequestMapping("sys/unit")
public class UnitController {
    @Autowired
    private UnitService unitService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:unit:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = unitService.queryPage(params);

        return R.ok().put("page", page);
    }
    /**
     * 列表
     */
    @RequestMapping("/findUnitDownList")
    public JSONArray findUnitDownList(@RequestParam Map<String, Object> params){
        String uName = Tools.dealNullStr(params.get("UName"));
        String pageSize = "15";
        String pageNo = "1";
        Object pageSizeObj = params.get("pageSize");
        Object pageNoObj = params.get("pageNo");
        if(pageSizeObj!=null){
            pageSize=pageSizeObj.toString();
        }
        if(pageNoObj!=null){
            pageNo=pageNoObj.toString();
        }
        params.put("limit",pageSize);
        params.put("page",pageNo);
        Page<UnitEntity> page = unitService.selectPage(
                new Query<UnitEntity>(params).getPage(),
                new EntityWrapper<UnitEntity>()
                        .like("uname",uName)
                        .orderBy("Id")
        );
        PageUtils pageUtils = new PageUtils(page);

        List<UnitEntity> dataList = (List<UnitEntity>) pageUtils.getList();

        //存放数据json数组
        JSONArray dataArray = new JSONArray();
        if (null != dataList) {
            for (UnitEntity unit : dataList) {
                JSONObject item = new JSONObject();
                item.put("id", unit.getId());
                //名称
                item.put("UName", unit.getUname());
                dataArray.add(item);
            }
        }

        return dataArray;
    }


    /**
     * 列表
     */
    @RequestMapping("/findBy")
    public R findBy(@RequestParam Map<String, Object> params){
        String uName = Tools.dealNullStr(params.get("UName"));
        String pageSize = "15";
        String pageNo = "1";
        Object pageSizeObj = params.get("pageSize");
        Object pageNoObj = params.get("pageNo");
        if(pageSizeObj!=null){
            pageSize=pageSizeObj.toString();
        }
        if(pageNoObj!=null){
            pageNo=pageNoObj.toString();
        }
        params.put("limit",pageSize);
        params.put("page",pageNo);
        Page<UnitEntity> page = unitService.selectPage(
                new Query<UnitEntity>(params).getPage(),
                new EntityWrapper<UnitEntity>()
                        .like("uname",uName)
                        .orderBy("Id")
        );
        PageUtils pageUtils = new PageUtils(page);

        List<UnitEntity> dataList = (List<UnitEntity>) pageUtils.getList();

        //存放数据json数组
        JSONArray dataArray = new JSONArray();
        if (null != dataList) {
            for (UnitEntity unit : dataList) {
                JSONObject item = new JSONObject();
                item.put("id", unit.getId());
                //名称
                item.put("UName", unit.getUname());
                item.put("op", 1);
                dataArray.add(item);
            }
        }

        return R.ok().put("rows",dataArray).put("total",dataList.size());
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("sys:unit:info")
    public R info(@PathVariable("id") Long id){
        UnitEntity unit = unitService.selectById(id);

        return R.ok().put("unit", unit);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("sys:unit:save")
    public R save(@RequestBody UnitEntity unit){
        unitService.insert(unit);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("sys:unit:update")
    public R update(@RequestBody UnitEntity unit){
        ValidatorUtils.validateEntity(unit);
        unitService.updateAllColumnById(unit);//全部更新
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("sys:unit:delete")
    public R delete(@RequestBody Long[] ids){
        unitService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }

}
